/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */

package org.openmrs.module.clinicalsummary.rule.primary;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Concept;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.logic.LogicContext;
import org.openmrs.logic.result.Result;
import org.openmrs.module.clinicalsummary.rule.EvaluableConstants;
import org.openmrs.module.clinicalsummary.rule.EvaluableNameConstants;
import org.openmrs.module.clinicalsummary.rule.EvaluableRule;
import org.openmrs.module.clinicalsummary.rule.observation.ObsWithRestrictionRule;
import org.openmrs.module.clinicalsummary.rule.observation.ObsWithStringRestrictionRule;

import java.util.Arrays;
import java.util.Map;

public class PositiveHivRule extends EvaluableRule {

	private static final Log log = LogFactory.getLog(PositiveHivRule.class);

	public static final String TOKEN = "Positive HIV";

	public static final String HIV_INFECTED = "HIV INFECTED";

	/**
	 * @param context
	 * @param patient
	 * @param parameters
	 * @return
	 * @see org.openmrs.logic.Rule#eval(org.openmrs.logic.LogicContext, org.openmrs.Patient, java.util.Map)
	 */
	@Override
	protected Result evaluate(final LogicContext context, final Patient patient, final Map<String, Object> parameters) {

		Result result = new Result();

		Concept positiveHivConcept = Context.getConceptService().getConcept(HIV_INFECTED);

		ObsWithRestrictionRule obsWithRestrictionRule = new ObsWithStringRestrictionRule();

		parameters.put(EvaluableConstants.OBS_FETCH_SIZE, 1);
		parameters.put(EvaluableConstants.OBS_CONCEPT, Arrays.asList("HIV RAPID TEST, QUALITATIVE", "HIV TEST RESULT PREVIOUSLY"));
		parameters.put(EvaluableConstants.OBS_VALUE_CODED, Arrays.asList(EvaluableNameConstants.POSITIVE));

		Result hivPositiveResults = obsWithRestrictionRule.eval(context, patient, parameters);
		if (CollectionUtils.isNotEmpty(hivPositiveResults))
			result.add(new Result(positiveHivConcept));

		return result;
	}

	/**
	 * Get the token name of the rule that can be used to reference the rule from LogicService
	 *
	 * @return the token name
	 */
	@Override
	protected String getEvaluableToken() {
		return TOKEN;
	}
}
